/*
 *    Copyright 2009-2010 The iBaGuice Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.google.code.ibaguice.samples;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.sql.SQLException;

import lombok.Setter;

import org.apache.ibatis.exceptions.IbatisException;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.google.code.ibaguice.dao.TransactionalModule;
import com.google.code.ibaguice.samples.SimpleContactDao;
import com.google.code.ibaguice.samples.ContactMapper;
import com.google.code.ibaguice.samples.DaoException;
import com.google.code.ibaguice.samples.TransactionalContactDao;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * 
 * 
 * @author Marco Speranza
 * @version $Id: SimpleTestCase_Mock.java 329 2009-12-25 21:32:01Z marco.speranza79 $
 */
public final class SimpleExceptionRethrowTestCase {

    @Setter
    @Inject
    private SimpleContactDao contactDao;

    @Setter
    @Inject
    private TransactionalContactDao transactionalContactDao;

    private SqlSessionFactory sqlSessionFactoryMock;

    @BeforeMethod
    public void setUp() throws Exception {
        this.sqlSessionFactoryMock = mock(SqlSessionFactory.class);
        AbstractModule absModule = new AbstractModule(){
            @Override
            protected void configure() {
                bind(SqlSessionFactory.class).toInstance(sqlSessionFactoryMock);
            }
        };
        Injector simpleInjector = Guice.createInjector(absModule,new TransactionalModule());
        simpleInjector.injectMembers(this);
    }

    @Test
    public void testSimpleDaoIbatisException() throws Exception {
        SqlSession sessionMock = mock(SqlSession.class);
        ContactMapper mapperMock = mock(ContactMapper.class);

        //stubbing
        when(this.sqlSessionFactoryMock.openSession()).thenReturn(sessionMock);
        when(sessionMock.getMapper(ContactMapper.class)).thenReturn(mapperMock);
        when(mapperMock.selectAll()).thenThrow(new IbatisException(new SQLException("Test")));

        try {
            this.contactDao.getAll();
            Assert.fail("Ibatis Layer exception not handled!");
        } catch (DaoException e) {
        }

        //verification
        verify(this.sqlSessionFactoryMock).openSession();
        verify(sessionMock, times(0)).commit(true);
        verify(mapperMock).selectAll();
    }

    @Test
    public void testTransactionalDaoIbatisException() throws Exception {
        SqlSession sessionMock = mock(SqlSession.class);
        ContactMapper mapperMock = mock(ContactMapper.class);

        //stubbing
        when(this.sqlSessionFactoryMock.openSession(ExecutorType.SIMPLE, TransactionIsolationLevel.SERIALIZABLE)).thenReturn(sessionMock);
        when(sessionMock.getMapper(ContactMapper.class)).thenReturn(mapperMock);
        when(mapperMock.selectAll()).thenThrow(new IbatisException(new SQLException("Stub SqlException")));

        try {
            this.transactionalContactDao.getAll();
            Assert.fail("Ibatis Layer exception not handled!");
        } catch (Exception e) {
        }

        //verification
        verify(this.sqlSessionFactoryMock).openSession(ExecutorType.SIMPLE, TransactionIsolationLevel.SERIALIZABLE);
        verify(sessionMock, times(0)).commit(true);
        verify(mapperMock).selectAll();
    }

}
